#!spider/Scripts/python.exe
import multiprocessing
import os
import sys
import time
import psutil
# from util.selenium import webdriver
# sys.path.append(r"C:\Users\Administrator\Desktop\Project\autoflow")
pro_path = r"C:\Users\Administrator\Desktop\Project\autoflow"
sys.path.append(pro_path)
# print(os.listdir(pro_path))
for i in os.listdir(pro_path):
    path = pro_path + "\\" + i
    # print(path)
    if os.path.isdir(path) and not i.startswith("\."):
        # print(path)
        sys.path.append(i)
        for j in os.listdir(path):
            file_path = path + "\\" + j
            if j.endswith(".py"):
                # print(file_path)
                sys.path.append(file_path)

from util.Logger import Logger
from settings import log_path, base_path


class g():

    num = 0
def kill(pid):
    # 本函数用于中止传入pid所对应的进程
    if os.name == 'nt':
        # Windows系统
        cmd = 'taskkill /pid ' + str(pid) + ' /f'
        try:
            os.system(cmd)
            print(pid, 'killed')
        except Exception as e:
            print(e)
    elif os.name == 'posix':
        # Linux系统
        cmd = 'kill ' + str(pid)
        try:
            os.system(cmd)
            print(pid, 'killed')
        except Exception as e:
            print(e)
    else:
        print('Undefined os.name')

def func(num=0):
    for i in range(num, num + 30):
        g.num = i
        time.sleep(0.06)
        pid = os.getpid()
        p_name = psutil.Process(pid).name()
        g.logger = Logger(log_file_name=log_path.replace("log.txt", "log_process.txt"), log_level=Logger.DEBUG,
                          logger_name="autoflow_{}".format(pid)).get_log()
        # print("{} --------- {} ------- {}".format(p_name, pid, g.num))
        g.logger.info("num: {}".format(i))
        if i == 115:
            os._exit(0)

# def sele_func(url):
#     driver = webdriver.Chrome()
#     if "baidu" in url:
#         os._exit()
#     driver.get(url)
#     print(url)
#     driver.quit()

if __name__ == '__main__':
    # import setproctitle
    #
    # setproctitle("python")
    #
    # pid = os.getpid()
    # p_name = psutil.Process(pid).name()
    # print(p_name, pid)



    pool = multiprocessing.Pool(processes=2)
    s = sys.argv
    while True:

        for i in s:
            print(i)
            time.sleep(0.3)
    # par = sys.argv
    # process = 0
    # # print(par)
    # if len(par) > 1:
    #     process = int(par[1])
    # while True:
    #     flag = psutil.pid_exists(process)
    #     if not flag:
    #         file = base_path.replace("/", "\\") + "\\test\\WindowOperateTest.py"
    #         cmd = "python {}".format(file)
    #         os.system(cmd)
    #         os._exit()